package com.mall.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class User extends BaseEntity implements UserDetails {

    private Integer userId;

    @NotBlank
    @ApiModelProperty("用户名")
    private String username;
    @Email
    @NotBlank
    @ApiModelProperty("邮箱")
    private String email;
    @NotBlank
    @ApiModelProperty("密码")
    private String password;

    @ApiModelProperty("角色标识")
    private String roleKey;

    @ApiModelProperty("头像地址")
    private String avatar;

    @NotBlank
    private String mailCode;

    @ApiModelProperty("电话号码")
    private String phonenumber;

    @ApiModelProperty("收货地址")
    private String shippingAddress;


    public Integer getUserId() {
        return userId;
    }

    @ApiIgnore
    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @ApiIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @ApiIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @ApiIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @ApiIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    @ApiIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<>();
        auths.add(this::getRoleKey);
        return auths;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getRoleKey() {
        return roleKey;
    }

    @ApiIgnore
    public void setRoleKey(String roleKey) {
        this.roleKey = roleKey == null ? null : roleKey.trim();
    }

    public String getMailCode() {
        return mailCode;
    }

    public void setMailCode(String mailCode) {
        this.mailCode = mailCode;
    }
}